package servlet;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import decoder.ChineseDecoder;

import model.Administrator;
import model.Manager;
import model.Member;
import model.Saler;

import exception.UnknownIdentityException;
import factory.DaoFactory;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LogInServlet")
public class LogInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LogInServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.sendRedirect(request.getContextPath() + "/jsp/LogIn.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String identity = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("identity"));
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		String password = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("password"));
		
		switch(identity) {
		
		case "member":
			Member member = DaoFactory.getMemberDao().find(name, password);
			if (member != null) {
				if (member.getLastRecharge() != null && Calendar.getInstance().getTimeInMillis() - member.getLastRecharge().getTime() > 2*365*24*3600000L) {
					response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=OutOfDate");
				} else {
					response.sendRedirect(request.getContextPath() + "/OrderGoodsServlet?name=" + name + "&date=day1");
				}
			} else {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=LogInFailure");
			}
			break;
			
		case "saler":
			Saler saler = DaoFactory.getSalerDao().find(name, password);
			if (saler != null) {
				response.sendRedirect(request.getContextPath() + "/SaleGoodsServlet?name=" + name);
			} else {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=LogInFailure");
			}
			break;
			
		case "manager":
			Manager manager = DaoFactory.getManagerDao().find(name, password);
			if (manager != null) {
				response.sendRedirect(request.getContextPath() + "/MemberCardStatisticsServlet");
			} else {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=LogInFailure");
			}
			break;
			
		case "administrator":
			Administrator administrator = DaoFactory.getAdministratorDao().find(name, password);
			if (administrator != null) {
				response.sendRedirect(request.getContextPath() + "/MemberManagementServlet");
			} else {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=LogInFailure");
			}
			break;
			
		default:
			throw new UnknownIdentityException();
			
		}
		
	}

}
